package com.ruiyian.eventnetty;

import com.ruiyian.common.core.redis.RedisCache;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * Socket拦截器，用于处理客户端的行为
 *
 * @author Gjing
 **/
@Slf4j
public class SocketHandler extends ChannelInboundHandlerAdapter {
    // redis
    @Autowired
    private RedisCache redisCache;

    // 创建客户端组
    public static final ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    // 事件服务器客户端保存
    public static final ChannelGroup evnetclients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    // 大屏客户端保存
    public static final ChannelGroup screenclients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    /**
     * 读取到客户端发来的消息
     *
     * @param ctx ChannelHandlerContext
     * @param msg msg
     * @throws Exception e
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.id());
        // 由于我们配置的是 字节数组 编解码器，所以这里取到的用户发来的数据是 byte数组
        byte[] data = (byte[]) msg;
        String msg_str = new String(data);
        int start = msg_str.indexOf("@") + 1;
        int end = msg_str.indexOf("/h264");
        String ipaddress = msg_str.substring(start, end); // ip地址
        char flag = msg_str.charAt(msg_str.length() - 1);
        System.out.println(ipaddress);
        System.out.println(flag);

        HashMap<String, Integer> neweventhashMap = new HashMap<>();
        neweventhashMap = redisCache.getCacheObject("eventdic");
        System.out.println(neweventhashMap.toString());
        neweventhashMap.put("eventflag", 1);
        redisCache.setCacheObject("eventdic", neweventhashMap);
        if(flag == '1'){
            neweventhashMap.put(ipaddress, 1);
        }
        else {
            neweventhashMap.put(ipaddress, 0);
        }

        redisCache.setCacheObject("eventdic", neweventhashMap);

//        channel.writeAndFlush(data);
//
//        if(new String(data) == ("event")){
//            evnetclients.add(ctx.channel());
//        }
//        else if(new String(data) == ("screen")){
//            screenclients.add(ctx.channel());
//        }



//        for (Channel client : clients) {
//            // 该函数实现向socket客户端发送数据
//            client.writeAndFlush(data);
//        }
        // 给其他人转发消息
//        for (Channel client : clients) {
//            if (!client.equals(ctx.channel())) {
//                client.writeAndFlush(data);
//            }
//        }
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("新的客户端链接：" + ctx.channel().id().asShortText());
        clients.add(ctx.channel());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        clients.remove(ctx.channel());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.channel().close();
        clients.remove(ctx.channel());
    }
}
